//
//  ZCSListenListSoundTableViewCell.m
//  聆听FM
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "ZCSListenListSoundTableViewCell.h"

@implementation ZCSListenListSoundTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backImageView.layer.cornerRadius = 23 * FITHEIGHT;
    self.headImageView.layer.cornerRadius = 20 * FITHEIGHT;
    self.headImageView.layer.masksToBounds = YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
//    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil];
//    CGRect newRect = [self.model.title boundingRectWithSize:CGSizeMake(240 * FITWIDTH, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
//    self.titleLabel.frame = CGRectMake(61 * FITWIDTH, 7 * FITHEIGHT, 246 * FITWIDTH, newRect.size.height);
}

- (void)setModel:(ZCSListenListListModel *)model
{
    if (_model != model) {
        _model = model;
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.coverSmall] placeholderImage:[UIImage sd_animatedGIFNamed:@"占位图"]];
        self.titleLabel.text = model.title;
        self.nikeLabel.text = [NSString stringWithFormat:@"by %@", model.nickname];
        self.chatLabel.text = [NSString stringWithFormat:@"%ld", model.commentsCounts];
        NSString *minString = [NSString stringWithFormat:@"%02ld", model.duration / 60];
        NSString *second = [NSString stringWithFormat:@"%02ld", model.duration % 60];
        self.playTimeLabel.text = [NSString stringWithFormat:@"%@:%@", minString, second];
        
        if (model.playsCounts > 10000) {
            self.playCountLabel.text = [NSString stringWithFormat:@"%.1f万", model.playsCounts / 10000.0];
        }
        else
        {
            self.playCountLabel.text = [NSString stringWithFormat:@"%ld", model.playsCounts];
        }
        
        //将字符串转化为整型并转换为NSDate
        NSInteger befor = model.createdAt;
        NSDate *beforTime = [NSDate dateWithTimeIntervalSince1970:befor];
        //得到距1970的时间差
        NSTimeInterval late=[beforTime timeIntervalSince1970];
        late = late / 1000;
        //得到现在的时间
        NSDate *dat = [NSDate dateWithTimeIntervalSinceNow:0];
        //得到现在时间距1970的时间差
        NSTimeInterval now = [dat timeIntervalSince1970];
        
        //初始化时间差字符串
        NSString *timeString = @"";
        //得到发表时间距现在的时间差
        NSTimeInterval cha = now - late;
        // NSLog(@"%.f, %.f, %.f", now, late, cha);
        //小于一小时
        if (cha/3600<1) {
            timeString = [NSString stringWithFormat:@"%f", cha/60];
            timeString = [timeString substringToIndex:timeString.length-7];
            timeString=[NSString stringWithFormat:@"%@分钟前", timeString];
        }
        //小于一天
        if (cha/3600>1&&cha/86400<1) {
            timeString = [NSString stringWithFormat:@"%f", cha/3600];
            timeString =      [timeString substringToIndex:timeString.length-7];
            timeString = [NSString stringWithFormat:@"%@小时前", timeString];
        }
        //大于一天
        if (cha/86400>1)
        {
            timeString = [NSString stringWithFormat:@"%f", cha/86400];
            timeString = [timeString substringToIndex:timeString.length-7];
            timeString = [NSString stringWithFormat:@"%@天前", timeString];
        }
        //大于一年
        if (cha / 31536000) {
            NSString *str= [NSString stringWithFormat:@"%ld",model.createdAt];//时间戳
            
            NSTimeInterval time=[str doubleValue]/1000;
            
            NSDate*detaildate=[NSDate dateWithTimeIntervalSince1970:time];
            
            //实例化一个NSDateFormatter对象
            
            NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];
            
            //设定时间格式,这里可以设置成自己需要的格式
            
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
            
            NSString*currentDateStr = [dateFormatter stringFromDate:detaildate];
            //  NSLog(@"%@", currentDateStr);
            
            timeString = [currentDateStr substringWithRange:NSMakeRange(0, 7)];
        }
        self.dataLabel.text = timeString;
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
